<?php

namespace app\index\controller;

use think\Controller;
use think\Cookie;
use think\exception\HttpResponseException;
use think\Request;
use think\Response;
use app\index\model\Config as ConfigModel;

class BaseController extends Controller
{
    protected $lang;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $basic = ConfigModel::getWebBasic();
        $request = Request::instance();
        $controller = $request->controller();
        $this->lang = Cookie::get('think_var');
        $this->assign('basic', $this->changeBasic($basic));
        $this->assign('controller', $controller);
        $this->assign('lang', $this->lang);
        header("Access-Control-Allow-Origin: *");
    }

    public function changeBasic($basic)
    {
        if ($this->lang == 'en') {
            $basic['address'] = $basic['address_en'];
            $basic['beian'] = $basic['beian_en'];
        } elseif ($this->lang == 'yue') {
            $basic['address'] = $basic['address_yue'];
            $basic['beian'] = $basic['beian_yue'];
        }
        return $basic;
    }

    public function changeResponses($responses)
    {
        foreach ($responses as &$response) {
            if ($this->lang == 'en' && $response['content_en']) {
                $response['content'] = $response['content_en'];
                $response['title'] = $response['title_en'];
                $response['description'] = $response['description_en'];
                $response['title_en'] = '';
            } elseif ($this->lang == 'yue' && $response['content_yue']) {
                $response['content'] = $response['content_yue'];
                $response['title'] = $response['title_yue'];
                $response['description'] = $response['description_yue'];
                $response['title_en'] = '';
            }
        }
        return $responses;
    }

    public function changeResponse($response)
    {
        if ($this->lang == 'en' && $response['content_en']) {
            $response['content'] = $response['content_en'];
            $response['title'] = $response['title_en'];
            $response['title_en'] = '';
        } elseif ($this->lang == 'yue' && $response['content_yue']) {
            $response['content'] = $response['content_yue'];
            $response['title'] = $response['title_yue'];
            $response['title_en'] = '';
        }
        return $response;
    }

    public function paginate($totalPage = 1, $currentPage = 1)
    {
        if ($totalPage <= 5) {
            $start = 1;
            $length = $totalPage;
        } elseif($totalPage - 4 <= $currentPage) {
            $start = $totalPage - 4;
            $length = $totalPage;
        } else {
            $start = $currentPage;
            $length = $currentPage + 4;
        }
        $this->assign('totalPage', $totalPage);
        $this->assign('currentPage', $currentPage);
        $this->assign('start', $start);
        $this->assign('length', $length);
    }


    /**
     * 默认响应输出类型,支持json/xml
     * @var string
     */
    protected $responseType = 'json';

    /**
     * 单参数验证
     * @param mixed $str
     */
    public function notEmpty($str)
    {
        $param = $this->request->param($str);
        return empty($param) ? $this->errorMsg($str . '不能为空') : $param;
    }

    public function successes($data = null, $msg = '请求成功')
    {
        $this->result($data, 0, $msg);
    }

    public function errorMsg($msg = '', $code = 1, $data = '')
    {
        $this->result($data, $code, $msg);
    }

    /**
     * 返回封装后的 API 数据到客户端
     * @access protected
     * @param mixed  $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码，默认为0
     * @param string $type   输出类型，支持json/xml/jsonp
     * @param array  $header 发送的 Header 信息
     * @return void
     * @throws HttpResponseException
     */
    protected function result($data = null, $code = 0, $msg = '',  $type = null, array $header = [])
    {
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'time' => Request::instance()->server('REQUEST_TIME'),
            'data' => $data,
        ];
        // 如果未设置类型则自动判断
        $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);

        if (isset($header['statuscode'])) {
            $code = $header['statuscode'];
            unset($header['statuscode']);
        } else {
            //未设置状态码,根据code值判断
            $code = $code >= 1000 || $code < 200 ? 200 : $code;
        }
        $response = Response::create($result, $type, $code)->header($header);
        throw new HttpResponseException($response);
    }


}
